package com.example.eduservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.commonsutils.R;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.vo.CoursePublishVO;
import com.example.eduservice.entity.vo.EduCourseInfoVO;
import com.example.eduservice.entity.vo.EduCourseQuery;
import com.example.eduservice.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@Tag(name = "课程信息控制器")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Resource
    private EduCourseService courseService;
    @Operation(description = "添加课程信息")
    @PostMapping("/")
    public R saveEduCourse(@RequestBody EduCourseInfoVO courseVO){
        String courseId = courseService.saveEduCourse(courseVO);
        return R.ok().data("courseId", courseId);
    }
    @Operation(description = "修改课程信息")
    @PutMapping("/")
    public R modifyCourseInfo(@RequestBody EduCourseInfoVO courseInfoVO){
        courseService.modifyCourseInfo(courseInfoVO);
        return R.ok();
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @Operation(description = "根据id查询课程信息")
    @GetMapping("/{courseId}")
    public R queryCourseInfoById(@PathVariable("courseId") String courseId){
        EduCourseInfoVO courseInfoVO = courseService.queryCourseInfoById(courseId);

        return R.ok().data("courseInfoVO", courseInfoVO);
    }
    @Operation(description = "根据课程id，查询课程发布信息")
    @GetMapping("/coursePublish/{courseId}")
    public R queryCoursePublishInfoById(@PathVariable("courseId") String courseId){
        CoursePublishVO publishVO = courseService.queryCoursePublishInfoById(courseId);
        return R.ok().data("publishVO", publishVO);
    }
    @Operation(description = "根据课程id，确认发布课程")
    @PutMapping("/publish/{courseId}")
    public R confirmPublishCourse(@PathVariable("courseId") String courseId){
        courseService.publishCourse(courseId);
        return R.ok();
    }
    @Operation(description = "查询课程列表")
    @GetMapping("/list")
    public R queryCourseList(){
        List<EduCourse> courseList = courseService.list();
        return R.ok().data("courseList", courseList);
    }
    @Operation(description = "条件查询课程列表带分页")
    @PostMapping ("/page/{begin}/{limit}")
    public R queryCourseListByConditionForPage(@PathVariable("begin") Integer begin,
                                    @PathVariable("limit") Integer limit,
                                    @RequestBody EduCourseQuery courseQuery){
        Page<EduCourse> page = courseService.queryCourseListByConditionForPage(begin, limit, courseQuery);
        return R.ok().data("total", page.getTotal()).data("courseList", page.getRecords());
    }
    @CacheEvict(cacheNames = "eduservice::front", key = "'popularCourse'")
    @Operation(description = "根据课程id，删除课程信息")
    @DeleteMapping("/{courseId}")
    public R removeCourseById(@PathVariable("courseId") String courseId){
        courseService.removeCourseById(courseId);
        return R.ok();
    }
}
